﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SharingCenter.Web.Shared
{
    using SharingCenter.Web.Classes;
    using SharingCenter.Web.Classes.enums;
    using SharingCenter.Web.Classes.Groups;
    
    public partial class GroupHeader : System.Web.UI.UserControl
    {
        Guid groupId;
        string groupIdQueryStringField;
        string pageName = string.Empty;
        string subTitle = string.Empty;
        public string PageName { get { return pageName; } set { pageName = value; } }
        public Guid GroupId { get { return groupId; } set { groupId = value; } }
        public string GroupIdQueryStringField { get { return groupIdQueryStringField; }
            set
            {
                try
                {
                    groupIdQueryStringField = value;
                    groupId = new Guid(Request.QueryString[value]);
                }
                catch { }
            } 
        }

        public string SubTitle { get { return subTitle; } set { subTitle = value; } }

        protected void Page_Init(object sender, EventArgs e)
        {
            FormView1.DataBound += new EventHandler(FormView1_DataBound);
        }

        void FormView1_DataBound(object sender, EventArgs e)
        {
            
            if (pageName != "" && pageName != null)
            {
                Label lblPageName = (Label)FormView1.FindControl("lblPageName");
                lblPageName.Text = pageName;
               
            }
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            FormView1.DataSource = scDataContext.Instance.GetGroup(groupId);
            FormView1.DataBind();
        }

        protected void lblSubTitle_PreRender(object sender, EventArgs e)
        {
            Label s = (Label)sender;
            if (subTitle != string.Empty && subTitle != null)
            {
                s.Text = subTitle;
            }
            else
            {
                s.Visible = false;
            }
        }

        protected void HyperLink1_PreRender(object sender, EventArgs e)
        {
            //((Control)sender).Visible = GroupOperations.IsUserInGroupRole(groupId, WebProfile.Current.UserId, GroupRole.GroupAdmin);

            Sc_GroupMember m = GroupOperations.GetGroupMember(groupId, WebProfile.Current.UserId);
            if (m != null)
            {
                ((Control)sender).Visible = m.AdminRole == GroupRole.Owner || m.AdminRole == GroupRole.GroupAdmin;
            }
        }
    }
}